//
//  Network.m
//  OR_1
//
//  Created by Luboš Kulič on 6/15/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Network.h"


@implementation Network



-(void)getData:(NSString *)dataURL notifyEnd:(id<NetworkEventListener>) listener_ {
	listener = listener_;
	
	NSURL *url = [NSURL URLWithString:dataURL];
	
	//TODO: error check (malformed URL...)
	
	NSURLRequest *request = [NSURLRequest requestWithURL:url];
	
	//NSURLResponse *response = [[NSURLResponse alloc] init];
	
	//NSError *error = [NSError alloc];
	
	//NSData * data = [[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] retain];
	
	[receivedData autorelease];
	receivedData = [[NSMutableData data]retain];
	
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	
	[NSURLConnection connectionWithRequest:request delegate:self];
	
	//NSString * result = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding];
						 
	
	//[response autorelease];
	//[error autorelease];
	
	// TODO - memory leaking??
	
	
	return ;
	
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	//NSLog(@"Received data, %@", [NSString stringWithUTF8String:[data bytes]]);
	[receivedData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
	NSLog(@"Did Finish Loading");
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
	[listener network:self didFinishLoadingWithResult:receivedData];
}


@end


//  28411391
























